package logic;

import io.TextFileReader;

import java.io.File;
import java.util.ArrayList;
import java.util.Properties;

import javax.swing.table.TableModel;

import properties.EPropertyFile;
import properties.PropertiesRetriever;
import utilities.MathUtilities;
import data.DataRecord;
import data.DataSet;
import data.DataEntry;
import data.InterviewContainer;
import data.Interview;
import data.InterviewerContainer;

public class InterconFacade {

	private static final int PC_AMOUNT = 460;
	private static final int PC_START = 400;
	private InterviewerContainer interList;
	private String DATAKEY_PATH;
	private InterviewContainer dataContainer;
	private String DATA_PATH;
	private String DELIMITER;
	private String PROJECT_PATH;
	
	public InterconFacade() {
		// Properties holen
		Properties sysprops   = System.getProperties();
		PropertiesRetriever propRetriever = null;
		if ( sysprops.getProperty( "os.name" ).equals( "Linux" ) ){
			propRetriever =
				new PropertiesRetriever( EPropertyFile.LINUX );
		}
		if ( sysprops.getProperty( "os.name" ).contains("Windows" ) ){
			propRetriever =
				new PropertiesRetriever( EPropertyFile.WINDOWS );
		}
	
		DATA_PATH = propRetriever.getProperty( "datapath" );
		DELIMITER = sysprops.getProperty( "file.separator" );
		PROJECT_PATH = propRetriever.getProperty( "projects" );
		String ZUGANG_PATH = propRetriever.getProperty( "zugang" );
		DATAKEY_PATH = propRetriever.getProperty( "datakey" );

		//InterviewerListe initialisieren
		interList = new InterviewerContainer( ZUGANG_PATH );
	}
	
	
	/**
	 * Liest die Daten ein.
	 */
	private void readData( String project ) {
		// Liste mit den einzulesenden Dateien erstellen
		ArrayList<String> paths = new ArrayList<String>();
		for( int i = PC_START; i <= PC_AMOUNT; i ++ ) {
			String datapath = new String();
			datapath = DATA_PATH + i + DELIMITER + project + "_.num";
			paths.add( datapath );
		}
		String datakey = DATAKEY_PATH + DELIMITER + project;
		dataContainer = new InterviewContainer();
		dataContainer.readFilesByKey( paths, datakey );

	}
	
	// Gesamtanalyse
	public String analyzeTotaltoString() {
		DataListFilter dataFilter = new DataListFilter();
		ArrayList<Integer> computerNummern = 
			dataFilter.findUniqueCompNr( dataContainer );	

		int gesGue = 0;
		int gesJa = 0;
		int gesNein = 0;
		int gesVerb = 0;
		int gesCount = 0;
		
		
		ArrayList< DataCounter > statsPCNR = new ArrayList();
		for( Integer i : computerNummern ) {
			
			DataCounter dataCounter = new DataCounter();
			InterviewContainer dataX = dataFilter.FilterByCompNr( i, dataContainer );
			dataCounter.reset();
			for( Interview data : dataX.getDataList()){
				dataCounter.check(data);
			}
			statsPCNR.add( dataCounter );
			dataCounter.getResultString();

		}
		for( DataCounter c : statsPCNR ) {
			gesGue += c.getGesamtGueltig();
			gesJa += c.getGesamtJa();
			gesNein += c.getGesamtNein();
			gesVerb += c.getGesamtVerbot();
		}
		gesCount = gesJa + gesNein;
		
		// Ausgabestring
		String output = new String();
		output += " Gesamt:\t" + gesCount + "\n";
		output += " Ja:\t" 
				+ MathUtilities.givePercent( gesJa, gesCount) + " %\n";
		output += " Nein:\t" 
				+ MathUtilities.givePercent( gesNein, gesCount) + " %\n";
		output += " Verbot:\t" 
				+ MathUtilities.givePercent( gesVerb, gesGue ) + " %";
		return output;
	}


	public TableModel getInterviewerList() {
		TableModel out = interList.getDataSet();
		return out;
	}

	public TableModel getAnalyzeByInterviewer( String project ) {
		readData( project );
		DataListFilter dataFilter = new DataListFilter();
		
		ArrayList<Integer> interviewerNummern = 
			dataFilter.findUniqueInterNr( dataContainer );	
		
		ArrayList< DataCounter > statsINTNR = new ArrayList<DataCounter>();
		DataSet data = new DataSet();
		for( Integer i : interviewerNummern ) {
			DataCounter dataCounter = new DataCounter();
			
			InterviewContainer container = 
				dataFilter.FilterByInterNr(i, dataContainer );
			dataCounter.reset();
			for( Interview interview : container.getDataList()){
				dataCounter.check( interview );
			}
			statsINTNR.add( dataCounter );
			dataCounter.getResultString();
			if( dataCounter.getGesamtGueltig() > 0 ) {
				DataRecord record = new DataRecord();
				DataEntry entry = 
					new DataEntry( "vorname", interList.getVornameByNr( i ));
				record.add( entry );
				entry = 
					new DataEntry( "name", interList.getNameByNr( i ));
				record.add( entry );
				DataRecord result = dataCounter.getResult();
				record.add( result );
				data.add( record );
			}
		}
		data.sortBy( "% Nein", Boolean.FALSE );
		return data;
	}
	
	public ArrayList<String> getProjects() {
		ArrayList<String> list = new ArrayList<String>();

		File keypath = new File( DATAKEY_PATH );
		File[] files = keypath.listFiles();
		for ( File f : files ) {
			list.add( f.getName() );
		}
		return list;
		
	}
}
